/*
 * HammingView.java
 */
package hamming;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;

/**
 * The application's main frame.
 */
public class HammingView extends FrameView {

    public HammingView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = HammingApp.getApplication().getMainFrame();
            aboutBox = new HammingAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        HammingApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        VnesiMatrica = new javax.swing.JButton();
        mainContentPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextArea2 = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        listaNaMatrici = new javax.swing.JButton();
        matrixTopic = new javax.swing.JLabel();
        btnTransposeMatrix = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(hamming.HammingApp.class).getContext().getResourceMap(HammingView.class);
        VnesiMatrica.setText(resourceMap.getString("VnesiMatrica.text")); // NOI18N
        VnesiMatrica.setName("VnesiMatrica"); // NOI18N
        VnesiMatrica.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                VnesiMatricaMouseClicked(evt);
            }
        });

        mainContentPanel.setName("mainContentPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTextArea1.setBackground(resourceMap.getColor("jTextArea1.background")); // NOI18N
        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setRows(5);
        jTextArea1.setText(resourceMap.getString("jTextArea1.text")); // NOI18N
        jTextArea1.setName("jTextArea1"); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jTextArea2.setBackground(resourceMap.getColor("jTextArea2.background")); // NOI18N
        jTextArea2.setColumns(20);
        jTextArea2.setEditable(false);
        jTextArea2.setRows(5);
        jTextArea2.setText(resourceMap.getString("jTextArea2.text")); // NOI18N
        jTextArea2.setName("jTextArea2"); // NOI18N
        jScrollPane2.setViewportView(jTextArea2);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        javax.swing.GroupLayout mainContentPanelLayout = new javax.swing.GroupLayout(mainContentPanel);
        mainContentPanel.setLayout(mainContentPanelLayout);
        mainContentPanelLayout.setHorizontalGroup(
            mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainContentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jScrollPane1)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 717, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addComponent(jSeparator1))
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        mainContentPanelLayout.setVerticalGroup(
            mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainContentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addContainerGap(131, Short.MAX_VALUE))
        );

        jSeparator2.setName("jSeparator2"); // NOI18N

        listaNaMatrici.setText(resourceMap.getString("listaNaMatrici.text")); // NOI18N
        listaNaMatrici.setName("listaNaMatrici"); // NOI18N
        listaNaMatrici.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaNaMatriciMouseClicked(evt);
            }
        });

        matrixTopic.setText(resourceMap.getString("matrixTopic.text")); // NOI18N
        matrixTopic.setName("matrixTopic"); // NOI18N

        btnTransposeMatrix.setText(resourceMap.getString("btnTransposeMatrix.text")); // NOI18N
        btnTransposeMatrix.setEnabled(false);
        btnTransposeMatrix.setName("btnTransposeMatrix"); // NOI18N
        btnTransposeMatrix.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                transposeMatrix(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 737, Short.MAX_VALUE)
                            .addComponent(mainContentPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(VnesiMatrica)
                        .addGap(18, 18, 18)
                        .addComponent(listaNaMatrici)
                        .addGap(18, 18, 18)
                        .addComponent(btnTransposeMatrix)
                        .addGap(18, 18, 18)
                        .addComponent(matrixTopic, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)))
                .addGap(65, 65, 65))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(VnesiMatrica)
                        .addComponent(listaNaMatrici)
                        .addComponent(btnTransposeMatrix))
                    .addComponent(matrixTopic, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 6, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainContentPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(hamming.HammingApp.class).getContext().getActionMap(HammingView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 590, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    public void addElementInMainContentPannel(JPanel panela, JButton button) {
        javax.swing.GroupLayout mainContentPanelLayout = new javax.swing.GroupLayout(mainContentPanel);
        mainContentPanel.setLayout(mainContentPanelLayout);
        mainContentPanelLayout.setHorizontalGroup(
                mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainContentPanelLayout.createSequentialGroup()
                .addGroup(mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainContentPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(mainContentPanelLayout.createSequentialGroup().addGap(152, 152, 152).addComponent(button))).addContainerGap(20, Short.MAX_VALUE)));
        mainContentPanelLayout.setVerticalGroup(
                mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(mainContentPanelLayout.createSequentialGroup().addGap(10, 10, 10).addComponent(panela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(button).addContainerGap(20, Short.MAX_VALUE)));
    }
    public void addElementInMainContentPannel2(JScrollPane lista, JButton button) {
        javax.swing.GroupLayout mainContentPanelLayout = new javax.swing.GroupLayout(mainContentPanel);
        mainContentPanel.setLayout(mainContentPanelLayout);
        mainContentPanelLayout.setHorizontalGroup(
                mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainContentPanelLayout.createSequentialGroup()
                .addGroup(mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainContentPanelLayout.createSequentialGroup().addContainerGap()
                .addComponent(lista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(mainContentPanelLayout.createSequentialGroup()
                .addGap(62, 62, 62)
                .addComponent(button)))
                .addContainerGap(20, Short.MAX_VALUE)));
        mainContentPanelLayout.setVerticalGroup(
                mainContentPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(mainContentPanelLayout.createSequentialGroup().addGap(10, 10, 10)
                .addComponent(lista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(button)
                .addContainerGap(20, Short.MAX_VALUE)));
    }
    public String imeNaMatrica;
    public String brojNaKoloni;
    public String brojNaRedovi;
    public EnterMatricaView helperPanel;

    private void VnesiMatricaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_VnesiMatricaMouseClicked
        btnTransposeMatrix.setEnabled(false);
        mainContentPanel.removeAll();
        statusMessageLabel.setText("");
        matrixTopic.setText("");
        //javax.swing.JPanel helperPanel;
        javax.swing.JButton sb;
        helperPanel = new EnterMatricaView();
        sb = new javax.swing.JButton();
        sb.setText("Внеси / Enter");
        sb.setName("SubmitVnesiMatrica");
        sb.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent ev) {
                ZapisiJaVoTextFileMatricata(ev);
            }
        });
        addElementInMainContentPannel(helperPanel, sb);


    }//GEN-LAST:event_VnesiMatricaMouseClicked
    public JList list;
    private void listaNaMatriciMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaNaMatriciMouseClicked
        clearMainContentPanel();
        btnTransposeMatrix.setEnabled(false);
        matrixTopic.setText("");
        statusMessageLabel.setText("Изберете некоја од веќе внесените матрици!");
        ReadWriteText fajl = new ReadWriteText();
        DefaultListModel listModel = new DefaultListModel();

        List<String> textFiles = new ArrayList<String>();
        textFiles = fajl.textFiles("."+File.separator+"Matrici"+File.separator);
        for (int i = 0; i < textFiles.size(); i++) {
            listModel.addElement(textFiles.get(i));
        }
        list = new JList(listModel); //data has type Object[]
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setLayoutOrientation(JList.VERTICAL);
        list.setVisibleRowCount(-1);
        JScrollPane listScroller = new JScrollPane(list);
        listScroller.setPreferredSize(new Dimension(180, 280));
        javax.swing.JButton sb;
        sb = new javax.swing.JButton();
        sb.setText("Употреби / Use");
        sb.setName("SubmitVnesiMatrica");
        sb.addMouseListener(new java.awt.event.MouseAdapter() {

            @Override
            public void mouseClicked(java.awt.event.MouseEvent ev) {
                IskoristiJaSlektiranataMatrica(ev);
            }
        });


        addElementInMainContentPannel2(listScroller, sb);
    }//GEN-LAST:event_listaNaMatriciMouseClicked

    private void transposeMatrix(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_transposeMatrix
        if (!"".equals(currentMatrixName)){
            ReadWriteText rwt = new ReadWriteText(currentMatrixName + ".txt");
            try {
                int[][] transposedMatrix = rwt.transposeRealMatrix();
                ReadWriteText transposedMatrixFile =new ReadWriteText(currentMatrixName+"_transposed.txt");
                if (transposedMatrixFile.fileExists()) {
                    transposedMatrixFile.file.delete();
                }
                if (transposedMatrixFile.createFile()) {
                    rwt.saveTransposedMatrixToFile(transposedMatrixFile.file, transposedMatrix);
                    clearMainContentPanel();
                    showCurentMatrix(currentMatrixName+"_transposed", "");
                }
            } catch (IOException ex) {
                Logger.getLogger(HammingView.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

    }//GEN-LAST:event_transposeMatrix

    private void IskoristiJaSlektiranataMatrica(java.awt.event.MouseEvent ev){
            int firstSelIx = list.getSelectedIndex();
            Object sel = list.getModel().getElementAt(firstSelIx);
            String matrixName = sel.toString();
            matrixName = matrixName.substring(0,matrixName.indexOf(".txt"));
            showCurentMatrix(matrixName.trim(),"");
            btnTransposeMatrix.setEnabled(true);
    }

    private void ZapisiJaVoTextFileMatricata(java.awt.event.MouseEvent ev) {
        imeNaMatrica = helperPanel.getInputImeNaMatrica().getText().trim();
        brojNaKoloni = helperPanel.getInputBrojNaKoloni().getText();
        brojNaRedovi = helperPanel.getInputBrojNaRedovi().getText();


        char[] ch = brojNaKoloni.toCharArray();
        char[] rh = brojNaRedovi.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            if (!Character.isDigit(ch[i])) {
                statusMessageLabel.setForeground(Color.RED);
                statusMessageLabel.setText("Мора да внесете број!");
                return;
            }
        }
        for (int i = 0; i < rh.length; i++) {
            if (!Character.isDigit(rh[i])) {
                statusMessageLabel.setForeground(Color.RED);
                statusMessageLabel.setText("Мора да внесете број!");
                return;
            }
        }

        if ("".equals(imeNaMatrica) || imeNaMatrica == null) {
            statusMessageLabel.setForeground(Color.RED);
            statusMessageLabel.setText("Мора да внесете име на матрицата!");
            return;
        }
        if ("".equals(brojNaKoloni) || brojNaKoloni == null) {
            statusMessageLabel.setForeground(Color.RED);
            statusMessageLabel.setText("Мора да внесете број на колони!");
            return;
        }
        if ("".equals(brojNaRedovi) || brojNaRedovi == null) {
            statusMessageLabel.setForeground(Color.RED);
            statusMessageLabel.setText("Мора да внесете број на редови!");
            return;
        }
        if (Integer.parseInt(brojNaKoloni) > 600 || Integer.parseInt(brojNaRedovi) > 600) {
            statusMessageLabel.setForeground(Color.RED);
            statusMessageLabel.setText("Внесете број помал од 600!");
            return;
        }

        ReadWriteText fajl = new ReadWriteText(imeNaMatrica + ".txt");
        if (!fajl.fileExists()) {
            if (fajl.createFile()) {
                fajl.insertEmptyMatrix(brojNaRedovi, brojNaKoloni);
                clearMainContentPanel();
                String status = "Успешно ја Внесовте Матрицата!";
                showCurentMatrix(imeNaMatrica,status);

            } else {
                statusMessageLabel.setForeground(Color.RED);
                statusMessageLabel.setText("Внесувањето не е успешно!");
            }
        } else {
            statusMessageLabel.setForeground(Color.RED);
            statusMessageLabel.setText("Веќе постои матрица со името кое го внесовте.!");

        }

    }

    public void clearMainContentPanel() {
        mainContentPanel.removeAll();
        mainContentPanel.repaint();
        mainContentPanel.revalidate();

    }

    public javax.swing.JTable jTable1;
    public javax.swing.JTable jTable2;
    public String currentMatrixName = "";
    
    public void showCurentMatrix(String matrixName, String status) {
        clearMainContentPanel();

        matrixTopic.setText("Матрица/Matrix: "+matrixName);
        statusMessageLabel.setForeground(Color.BLACK);
        statusMessageLabel.setText(status + "Двоен клик на некое поле од табелата за да го промените.\r\n Можете да внесувате само 1 или 0.!");
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        javax.swing.JScrollPane jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        jTable1 = new javax.swing.JTable();

        jTable2 = new javax.swing.JTable();
        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane2.setName("jScrollPane2"); // NOI18N
        ReadWriteText fajl = new ReadWriteText(matrixName + ".txt");
        try {
            int rowNum = fajl.getNumberOfRows();
            int colNum = fajl.getNumberOfColumns();
            String[] columnTitles = new String[colNum];
            String[] rowTitles = new String[rowNum];
            for (int i = 0; i < colNum; i++) {
                int coltit = i + 1;
                columnTitles[i] = Integer.toString(coltit);

            }
            for (int i = 0; i < rowNum; i++) {
                int coltit1 = i + 1;
                rowTitles[i] = Integer.toString(coltit1);

            }
            Object[][] matrix = fajl.getMatrixContent();
            Object[][] qMatrix = fajl.presmetajQMatrica();
            float[] t = fajl.calculateProbability(matrix);
            HashMap hm = fajl.verojatnostNaOdredenaSlicnost(qMatrix, t);
            String vos = fajl.convertVerojatnostNOS(hm);
            fajl.presmetajPremini(matrix);
            fajl.verojatnostNaSosednaDistanca();

            javax.swing.JSeparator verticalSeparator = new javax.swing.JSeparator();
            verticalSeparator.setOrientation(javax.swing.SwingConstants.VERTICAL);
            verticalSeparator.setName("verticalSeparator");


            jTable1.setModel(new javax.swing.table.DefaultTableModel(matrix, columnTitles));
            jTable1.setName("jTable1"); // NOI18N
            jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

            jTable2.setModel(new javax.swing.table.DefaultTableModel(qMatrix, rowTitles));
            jTable2.setName("jTable2"); // NOI18N
            jTable2.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
           
            jScrollPane1.setViewportView(jTable1);
            JTable rowTable = new RowNumberTable(jTable1,30);
            jScrollPane1.setRowHeaderView(rowTable);
            jScrollPane1.setCorner(JScrollPane.UPPER_LEFT_CORNER,rowTable.getTableHeader());

            jScrollPane2.setViewportView(jTable2);
            JTable rowTable1 = new RowNumberTable(jTable2,40);
            jScrollPane2.setRowHeaderView(rowTable1);
            jScrollPane2.setCorner(JScrollPane.UPPER_LEFT_CORNER,rowTable.getTableHeader());




            JButton vnesiPromeni = new JButton();
            vnesiPromeni.setText("Пресметај / Calculate");
            vnesiPromeni.setName("vnesiPromeniMatrica");
            currentMatrixName = matrixName;
            vnesiPromeni.addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent ev) {
                    ZapisiGiPromeniteVoTextFileMatricata(ev);
                }
            });

            /*---------------------------------- Crtanjeto na tabelata i kopcinjata od strana -----------------------*/
            JLabel realnaMatrica = new JLabel();
            realnaMatrica.setText("<html>Реална Матрица:<br>Real Matrix:</html>");
            JLabel koef1 = new JLabel();
            koef1.setText("<html>Коефициент на целосна биомеханичка сличност (КЦБС):<br>Coefficient of full biomechanical similarity (CFBS):</html>");
            
            JLabel koef2 = new JLabel();
            koef2.setText("<html>Коефициент на соседна биомеханичка сличност (КСБС):<br>Coefficient of neighboring biomechanical similarity (CNBS):</html>");
            JLabel koef3 = new JLabel();
            koef3.setText("<html>Коефициенти на сила на биомеханичка поврзаност (КСБП):<br>Coefficient of biomechanical connection force (CBCF):</html>");
            JLabel koef1rez = new JLabel();
            String k1 = fajl.koef1();
            koef1rez.setText(k1);
            JLabel koef2rez = new JLabel();
            String k2 = fajl.koef2();
            koef2rez.setText(k2);
            String k3 = fajl.koef3();

            JLabel QMatrica = new JLabel();
            QMatrica.setText("<html>Q-Матрица:<br>Q-Matrix:</html>");

            JTextPane jTextPane1 = new JTextPane();
            jTextPane1.setName("jTextPane1"); // NOI18N
            jTextPane1.setText(k3);
            JScrollPane jScrollPane3 = new JScrollPane();
            jScrollPane3.setViewportView(jTextPane1);

            JLabel apVerojatnost = new JLabel();
            String apValue = fajl.apsolutnaVerojatnost(t);
            apVerojatnost.setText("<html>Апсолутна Веројатност:<br><br>"+fajl.getApsolutnaVerojatnost()+"</html>");

            JLabel vosLabel = new JLabel();
            vosLabel.setText("<html>Веројатност на одредена сличност:</html>");
            JTextPane jTextPane2 = new JTextPane();
            jTextPane2.setName("jTextPane2"); // NOI18N
            jTextPane2.setText(vos);
            JScrollPane jScrollPane4 = new JScrollPane();
            jScrollPane4.setViewportView(jTextPane2);

            JLabel avSlicnost = new JLabel();
            avSlicnost.setText("<html>Авто сличност:</html>");
            JTextPane jTextPane3 = new JTextPane();
            jTextPane3.setText(fajl.avtoSlicnost());
            JScrollPane jScrollPane5 = new JScrollPane();
            jScrollPane5.setViewportView(jTextPane3);

            JLabel v_ostan = new JLabel();
            v_ostan.setText("<html>Веројатност на останување:</html>");
            JTextPane jTextPane4 = new JTextPane();
            jTextPane4.setText(fajl.verojatnost_na_ostanuvanje);
            JScrollPane jScrollPane6 = new JScrollPane();
            jScrollPane6.setViewportView(jTextPane4);

            JLabel v_premin = new JLabel();
            v_premin.setText("<html>Веројатност на премин:</html>");
            JTextPane jTextPane5 = new JTextPane();
            jTextPane5.setText(fajl.verojatnost_na_premin);
            JScrollPane jScrollPane7 = new JScrollPane();
            jScrollPane7.setViewportView(jTextPane5);

            JLabel difuzija = new JLabel();
            String difuzija_val = fajl.difuzija;
            difuzija.setText("<html>Дифузија (Средна веројатност на премин):<br><br>D="+difuzija_val+"</html>");

            JLabel v_distanca = new JLabel();
            v_distanca.setText("<html>Веројатност на соседна дистанца (сличност):</html>");
            JTextPane jTextPane6 = new JTextPane();
            jTextPane6.setText(fajl.verojatnost_na_distanca);
            JScrollPane jScrollPane8 = new JScrollPane();
            jScrollPane8.setViewportView(jTextPane6);
            //JScrollPane jScrollPaneHolder = new JScrollPane();
//            jScrollPaneHolder.ad

            JButton zapisiRez = new JButton("Зачувај резултат / Save result");
            zapisiRez.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    ZapisiGiRezultatiteVoTextFileMatricata(evt);
                }
            });

            //Crtanje na lefata panela
            JPanel leftPanel = new JPanel();
            javax.swing.GroupLayout leftPanelLayout = new javax.swing.GroupLayout(leftPanel);
            leftPanel.setLayout(leftPanelLayout);
            leftPanelLayout.setHorizontalGroup(
                        leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(leftPanelLayout.createSequentialGroup()
                            .addContainerGap()
                            .addGroup(leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(realnaMatrica)
                                .addComponent(QMatrica)
                            )
                        )
                    );
            leftPanelLayout.setVerticalGroup(
                    leftPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(leftPanelLayout.createSequentialGroup()
                        .addComponent(realnaMatrica)
                        .addGap(17, 17, 17)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(17, 17, 17)
                        .addComponent(QMatrica)
                        .addGap(17, 17, 17)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                        )
                    );

            //Crtanje na desnata strana.
            JPanel rightPanel = new JPanel();
            javax.swing.GroupLayout rightPanelLayout = new javax.swing.GroupLayout(rightPanel);
            rightPanel.setLayout(rightPanelLayout);
            rightPanelLayout.setHorizontalGroup(
                rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(rightPanelLayout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addComponent(vnesiPromeni)
                                .addGap(36, 36, 36)
                                .addComponent(zapisiRez)
                             )
//                             .addComponent(scrollComponent)
                            .addGroup(rightPanelLayout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(rightPanelLayout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(koef1rez))
                                    .addComponent(koef1)
                                    .addComponent(koef2)
                                    .addGroup(rightPanelLayout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(koef2rez))
                                     .addComponent(koef3)
                                     .addComponent(apVerojatnost)
                                     .addComponent(vosLabel)
                                     .addComponent(avSlicnost)
                                     .addComponent(v_ostan)
                                     .addComponent(v_premin)
                                     .addComponent(difuzija)
                                     .addComponent(v_distanca)
                                )
                             )
                  )
            );
            rightPanelLayout.setVerticalGroup(
                    rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(rightPanelLayout.createSequentialGroup()
                                .addGroup(rightPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(vnesiPromeni)
                                    .addComponent(zapisiRez)
                                    )
//                                    .addComponent(scrollComponent)
                                .addGap(29, 29, 29)
                                .addComponent(koef1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(koef1rez)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(koef2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(koef2rez)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(koef3)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(apVerojatnost)
                                .addGap(18, 18, 18)
                                .addComponent(vosLabel)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(avSlicnost)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(v_ostan)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(v_premin)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(difuzija)
                                .addGap(18, 18, 18)
                                .addComponent(v_distanca)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane8, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                )
                    );

            JScrollPane rightSrcorlPane = new JScrollPane();
            rightSrcorlPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            rightSrcorlPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
            
             rightSrcorlPane.setViewportView(rightPanel);

            javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainContentPanel);
            mainContentPanel.setLayout(mainPanelLayout);
            
            mainPanelLayout.setHorizontalGroup(
                    mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                          .addComponent(leftPanel)
                        )
                    .addContainerGap()
                    .addGap(28, 28, 28)
                    .addComponent(verticalSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rightSrcorlPane)
                      ));
            mainPanelLayout.setVerticalGroup(
                    mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGap(20, 20, 20)
                    .addComponent(verticalSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                    .addComponent(rightSrcorlPane)
                    .addComponent(leftPanel)
                  )
           );


        } catch (IOException ex) {
            Logger.getLogger(HammingView.class.getName()).log(Level.SEVERE, null, ex);
        }


    }
    

    private void ZapisiGiPromeniteVoTextFileMatricata(java.awt.event.MouseEvent ev){
            clearMainContentPanel();
            ReadWriteText fajl = new ReadWriteText(currentMatrixName + ".txt");
            fajl.promeniJaMatricata(jTable1);
            String status = "Успешно ја Променивте Матрицата!";
            showCurentMatrix(currentMatrixName,status);
            btnTransposeMatrix.setEnabled(true);
    }
    private void ZapisiGiRezultatiteVoTextFileMatricata(java.awt.event.MouseEvent ev){
            clearMainContentPanel();
            ReadWriteText fajl = new ReadWriteText(currentMatrixName + ".txt");
            fajl.ZapisiRezultati(jTable1,currentMatrixName);
            String status = "Резултатите се запишани во Print/"+currentMatrixName + ".txt.";
            showCurentMatrix(currentMatrixName,status);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton VnesiMatrica;
    private javax.swing.JButton btnTransposeMatrix;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextArea jTextArea2;
    private javax.swing.JButton listaNaMatrici;
    private javax.swing.JPanel mainContentPanel;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel matrixTopic;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
}
